/* Copyright (c) 2010-2020 Vadim Surov.
 */
package com.srv.client.widget;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.google.gwt.event.logical.shared.BeforeSelectionEvent;
import com.google.gwt.event.logical.shared.BeforeSelectionHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.core.client.dom.ScrollSupport.ScrollMode;
import com.sencha.gxt.dnd.core.client.DndDropEvent;
import com.sencha.gxt.dnd.core.client.DropTarget;
import com.sencha.gxt.dnd.core.client.DND.Feedback;
import com.sencha.gxt.dnd.core.client.DND.Operation;
import com.sencha.gxt.widget.core.client.TabItemConfig;
import com.sencha.gxt.widget.core.client.TabPanel;
import com.sencha.gxt.widget.core.client.Dialog.PredefinedButton;
import com.sencha.gxt.widget.core.client.box.ConfirmMessageBox;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.event.HideEvent;
import com.sencha.gxt.widget.core.client.event.HideEvent.HideHandler;
import com.srv.shared.Card;
import com.srv.shared.Control;
import com.srv.shared.SetInfo;

public class SetListPanel extends VerticalLayoutContainer implements SetView {

	private boolean isSafeToClose = true;

	/**
	 * @param model
	 *          the model to which the Ui will bind itself
	 */
	public SetListPanel(final SetInfo setInfo, List<Card> cards) {
		super();
		setScrollMode(ScrollMode.AUTOY);
		setCards(cards);
	}

	private void setCards(List<Card> cards) {
		for (int i=0; i<cards.size(); ++i) 
			add(createCardPanel(cards.get(i), ""+i));
	}		

	private TabPanel createCardPanel(final Card card, final String name) {
		final TabPanel tabPanel = new TabPanel();

		final CardPanel cardPanel = new CardPanel(card);
		tabPanel.add(cardPanel, new TabItemConfig(name, false));

		final CardSourcePanel cardSourcePanel = new CardSourcePanel();
		tabPanel.add(cardSourcePanel, new TabItemConfig("Source", false));

		tabPanel.addSelectionHandler(new SelectionHandler<Widget>() {
			@Override
			public void onSelection(SelectionEvent<Widget> event) {
				TabPanel panel = (TabPanel) event.getSource();
				Widget w = event.getSelectedItem();
				TabItemConfig config = panel.getConfig(w);
				if (config.getText().equals("Source")) {
					cardSourcePanel.setValue(cardPanel.getCard());
					cardSourcePanel.forceLayout();
				}	
			}});		

		tabPanel.addBeforeSelectionHandler(new BeforeSelectionHandler<Widget>() {
			@Override
			public void onBeforeSelection(final BeforeSelectionEvent<Widget> event) {
				final TabPanel panel = (TabPanel) event.getSource();
				Widget w  = panel.getActiveWidget();
				TabItemConfig config = panel.getConfig(w);
				if (config.getText().equals("Source")) {
					if (!cardSourcePanel.isSafeToClose()) {
						event.cancel();
						final ConfirmMessageBox confirmMessageBox = new ConfirmMessageBox (
								"Confirm message", 
								"Modified source code cannot be applyed. " +
								"Are you sure you want to leave this tab anyway?");
						confirmMessageBox.setWidth(300);
						confirmMessageBox.addHideHandler(new HideHandler() {
							@Override
							public void onHide(HideEvent _event) {
								if (confirmMessageBox.getHideButton() == 
										confirmMessageBox.getButtonById(PredefinedButton.YES.name())) {
									cardSourcePanel.setSafeToClose(true);
									panel.setActiveWidget(cardPanel);
								}
							}
						});
						confirmMessageBox.show();	
					} else { 
						final List<Control> controls = new ArrayList<Control>();
						for (Control c : cardSourcePanel.getControls())
							controls.add(c.copy());
						cardPanel.setCard(new Card(cardSourcePanel.getFloating(), controls));
					}
				}	
			}});

		new DropTarget(tabPanel) {
			{
				setGroup("movecard");
				setFeedback(Feedback.INSERT);
				setOperation(Operation.COPY);
				setAllowSelfAsSource(true); 
			}
			@Override  
			protected void onDragDrop(DndDropEvent event) {  
				super.onDragDrop(event);  
				final List<?> dropcards = (List<?>)event.getData();
				final Card card = ((Card)dropcards.get(0)).copy();

				//final CardPanel cardPanel = new CardPanel(card);
				//final CardPanel activeCardPanel = (CardPanel) SetTabPanel.this.getActiveWidget();
				final int i = SetListPanel.this.getWidgetIndex(tabPanel);
				SetListPanel.this.insert(createCardPanel(card, "*"+i), i);
			}			
		};	

		return tabPanel;
	}

	@Override
	public boolean isSafeToClose() {
		return isSafeToClose;
	}

	@Override
	public List<Card> getCards() {
		final List<Card> cards = new LinkedList<Card>();
		for (int i=0, len = getWidgetCount(); i<len; ++i) {
			final TabPanel tabPanel = (TabPanel)getWidget(i); 
			final CardPanel cardPanel = (CardPanel) tabPanel.getWidget(0); 
			cards.add(cardPanel.getCard());
		}
		return cards;
	}
}	


